package com.kindsoft.demo.ad;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper;
import org.springframework.stereotype.Component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by zhoujm on 2020/2/21/021.
 */
@Component
public class AdUserDetailsMapper extends LdapUserDetailsMapper {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
        log.info("<mapUserFromContext> username=" + username);
        List<SimpleGrantedAuthority> WhAuthorities = new ArrayList<>();
        // 新建N个角色
        WhAuthorities.add(new SimpleGrantedAuthority("wh01"));
        WhAuthorities.add(new SimpleGrantedAuthority("wh02"));
        WhAuthorities.add(new SimpleGrantedAuthority("wh03"));
        return super.mapUserFromContext(ctx, username, WhAuthorities);
    }
}